package com.jxs.android.ges;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.ges.data.CaseInfo;
import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.OpTimer;
import com.jxs.android.util.Timer;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.Bu_A_Case_Item;
import com.jxs.android.weiget.TopBar;

public class A_Case_Page extends LoadingMoreAbstractList {

	private final int current_PageSize = 15;

	private String manager_id = "";// 客户经理ID
	private String cst_id = "";// 客户ID（多条件查询，可空）
	private OpTimer opTimer = new OpTimer(1);
	private String start_date = opTimer.getStartTime();// 开始日期(格式：yyyymmdd,例如：20120313)（多条件查询，可空）
	private String end_date = opTimer.getEndTime();// 结束日期(格式：yyyymmdd,例如：20120313)（多条件查询，可

	private CaseInfoAdapter adapter;

	@Override
	public List<CaseInfo> paserData(SoapObject retMsgObject, Object... params) {
		List<CaseInfo> noticeListInfos = new ArrayList<CaseInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CaseInfo cstInfo = new CaseInfo();
				cstInfo.create_date = SoapUtil.getValue(taskBeanObject, "create_date");
				cstInfo.cust_id = SoapUtil.getValue(taskBeanObject, "cust_id");
				cstInfo.cust_manager_name = SoapUtil.getValue(taskBeanObject, "cust_manager_name");
				cstInfo.cust_name = SoapUtil.getValue(taskBeanObject, "cust_name");
				cstInfo.sys_no = SoapUtil.getValue(taskBeanObject, "sys_no");
				noticeListInfos.add(cstInfo);
			}
		}
		return noticeListInfos;
	}

	@Override
	public String getMethod() {
		// 2.6.2 getCrmCasePage 【一户一案分页查询】
		return "getCrmCasePage";
	}

	private TopBar topBar;

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		PAGE_SIZE = current_PageSize;
		getWebServiceData(GesApplication.getUserInfo().getTicket(), Which.A_CASE_PAGE, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, manager_id, cst_id, start_date, end_date, PAGE_SIZE, currentPage);
	}

	private void initExtends(LinearLayout bu_common_extends) {
		Bu_A_Case_Item item = new Bu_A_Case_Item(this);
		item.setValue("序号", "客户", "客户\n经理", "更新\n日期");
		item.setBackGroud(R.drawable.row0);
		item.setTextColor(getResources().getColor(R.color.white));
		item.setCustNameGravityCenter();
		bu_common_extends.addView(item);
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_with_list;
	}

	@Override
	protected void extendsLayout() {
		super.extendsLayout();
		initExtends((LinearLayout) findViewById(R.id.bu_common_extends));
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		this.topBar = topBar;
		this.topBarReturn(topBar);
		this.topBar.setTitle(R.string.home_home_a_case);
		this.topBar.setFilterBtnText(R.string.prompt_filter);
		this.topBar.hindSpinnerBtn();
		this.topBar.setFilterBtnVisibility(View.VISIBLE);

		topBar.setBtnListern(new TopBar.BtnListern() {

			@Override
			@Deprecated
			public void onSpinnerBtn() {
			}

			@Override
			@Deprecated
			public void onLogicBtn() {
			}

			@Override
			public void onFilterBtn() {
				Intent intent = new Intent(A_Case_Page.this, Bu_Filter_Page.class);
				Bundle bundle = new Bundle();
				bundle.putString("startTime", start_date);
				bundle.putString("endTime", end_date);
				bundle.putString("cst_id", cst_id);
				bundle.putString("manager_id", manager_id);
				intent.putExtras(bundle);
				startActivityForResult(intent, FILTER_REQ);
			}
		});
	}

	final int FILTER_REQ = 0xa002;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (FILTER_REQ == requestCode && resultCode == RESULT_OK) {//
			Bundle bundle = data.getExtras();
			manager_id = bundle.getString("manager_id");
			cst_id = bundle.getString("cst_id");
			start_date = bundle.getString("startTime");
			end_date = bundle.getString("endTime");
			currentPage = 1;
			loadingData(currentPage, true);
		}
	}

	@Override
	public void updateList(Message msg) {
		@SuppressWarnings("unchecked")
		List<CaseInfo> tems = ((List<CaseInfo>) msg.obj);
		refreshListView.onRefreshComplete();
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreButton.setText("数据已全部获取完毕");
				loadMoreButton.setEnabled(false);
			}
		}
		adapter.setCaseInfos(tems);
		adapter.notifyDataSetChanged();

		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			loadMoreButton.setEnabled(true);
			currentPage++;
		}
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CaseInfoAdapter(context);
		return adapter;
	}

	class CaseInfoAdapter extends BaseAdapter {

		List<CaseInfo> wrkInfos = new ArrayList<CaseInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CaseInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCaseInfos(List<CaseInfo> custInfos) {
			if (currentPage == 1) {
				this.wrkInfos.clear();
			}
			this.wrkInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return wrkInfos.size();
		}

		@Override
		public CaseInfo getItem(int position) {
			return wrkInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final CaseInfo custInfo = wrkInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_a_case, null);
				holder = new ViewHolder();
				holder.index = (TextView) convertView.findViewById(R.id.index);
				holder.create_date = (TextView) convertView.findViewById(R.id.create_date);
				holder.cust_manager_name = (TextView) convertView.findViewById(R.id.cust_manager_name);
				holder.cust_name = (TextView) convertView.findViewById(R.id.cust_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.index.setText(String.valueOf(position + 1));
			holder.cust_manager_name.setText(custInfo.cust_manager_name);
			holder.cust_name.setText(custInfo.cust_name);
			String daStr = "";
			try {
				Date date = Timer.getSDFyyyy_MM_dd_hanzi().parse(custInfo.create_date);
				daStr = Timer.getSDFyy_MM_dd().format(date);
			} catch (Exception e) {
				daStr = "";
			}
			holder.create_date.setText(daStr);

			if (position % 2 == 0) {
				holder.index.setBackgroundResource(R.drawable.table_row0);
				holder.cust_manager_name.setBackgroundResource(R.drawable.table_row0);
				holder.cust_name.setBackgroundResource(R.drawable.table_row0);
				holder.create_date.setBackgroundResource(R.drawable.table_row0);
			} else {
				holder.index.setBackgroundResource(R.drawable.table_row1);
				holder.cust_manager_name.setBackgroundResource(R.drawable.table_row1);
				holder.cust_name.setBackgroundResource(R.drawable.table_row1);
				holder.create_date.setBackgroundResource(R.drawable.table_row1);
			}

			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(A_Case_Page.this, A_Case_HomePages.class);
					Bundle bundle = new Bundle();
					bundle.putString(A_Case_HomePages.TAG_SYS_NO, custInfo.sys_no);
					bundle.putString(A_Case_HomePages.TAG_CUST_ID, custInfo.cust_id);
					bundle.putString(A_Case_HomePages.TAG_SUB_TITLE, custInfo.cust_name);
					intent.putExtras(bundle);
					startActivity(intent);

				}
			});

			return convertView;
		}

		class ViewHolder {

			TextView index;// 客户ID
			TextView create_date;// 更新日期
			TextView cust_manager_name;// 客户经理
			TextView cust_name;// 客户名称
		}

	}

}
